﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace TY.UC.Common
{
    /// <summary>
    /// 常用小方法
    /// </summary>
    public static class Utility
    {
        #region CreateAuthCode 返回纯数字的验证码
        /// <summary>
        /// 返回纯数字的验证码
        /// </summary>
        /// <param name="maxlength"></param>
        /// <returns></returns>
        public static string CreateAuthCode(int maxlength)
        {
            string authCode = "";
            for (int i = 0; i < maxlength; i++)
            {
                Random r = new Random(BitConverter.ToInt32(Guid.NewGuid().ToByteArray(), 0));
                int seed = r.Next(0, 9);
                authCode += seed;
            }
            return authCode;
        }
        #endregion

        #region GetMoneyString 金额格式化
        /// <summary>
        /// 金额格式化
        /// </summary>
        /// <param name="m">金额值</param>
        /// <returns></returns>
        public static string GetMoneyString(string m)
        {
            if (string.IsNullOrEmpty(m))
            {
                return m;
            }
            double d;
            double.TryParse(m, out d);
            return d.ToString("0.00");
        }
        #endregion

        #region SortAsASCII 字典key按assic排序
        /// <summary>
        /// 字典key按assic排序
        /// </summary>
        /// <param name="dic">字典</param>
        /// <returns></returns>
        public static Dictionary<string, object> SortAsAscii(this Dictionary<string, object> dic)
        {
            if (dic == null)
                return null;
            var key = dic.Keys.ToList();
            key.Sort(StringCompare);
            return key.ToDictionary(item => item, item => dic[item]);
        }
        #endregion
        #region StringCompare 比较两个字符串大小（按assic码表排序）
        /// <summary>
        /// 比较两个字符串大小（按assic码表排序）
        /// </summary>
        /// <param name="x">字符串1</param>
        /// <param name="y">字符串2</param>
        /// <returns>x==y return 0 x大于y return 1 x小于y return -1</returns>
        private static int StringCompare(string x, string y)
        {
            if (x == null)
            {
                if (y == null)
                {
                    return 0;
                }
                return -1;
            }
            if (y == null)
            {
                return 1;
            }
            int xchar = 0;
            int ychar = 0;
            for (var i = 0; i <= x.Length; i++)
            {
                xchar = 0;
                ychar = 0;
                if (i < x.Length)
                {
                    xchar = x[i];
                }
                if (i < y.Length)
                {
                    ychar = y[i];
                }
                if (xchar == 0 || ychar == 0)
                {
                    break;
                }
                if (xchar == ychar)
                {
                    continue;
                }
                break;
            }
            return xchar <= ychar ? -1 : 1;
        }

        #endregion

        #region 扩展保留一位小数的double类型数据(直接丢弃小数点第二位后面的不是四舍五入)
        /// <summary>
        /// float扩展保留一位小数的double类型数据(直接丢弃小数点第二位后面的不是四舍五入)
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        public static double GetNumber(this float f)
        {
            string str = f.ToString();

            if (str.Contains("."))
                return Convert.ToDouble(str.Substring(0, str.IndexOf(".") + 2));

            return Convert.ToDouble(str);
        } 

        /// <summary>
        /// double扩展保留只有一位小数的double类型数据(直接丢弃小数点第二位后面的不是四舍五入)
        /// </summary>
        /// <param name="f"></param>
        /// <returns></returns>
        public static double GetNumber(this double f)
        {
            string str = f.ToString();

            if (str.Contains("."))
                return Convert.ToDouble(str.Substring(0, str.IndexOf(".") + 2));

            return Convert.ToDouble(str);
        } 
        #endregion

        #region  LinuxTimeStamp 获取linux时间戳
        /// <summary>
        /// 获取linux时间戳
        /// </summary>
        /// <param name="time">时间</param>
        /// <returns>时间戳</returns>
        public static long LinuxTimeStamp(DateTime? time=null)
        {
            if (time == null) time = DateTime.Now;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Value.Ticks - startTime.Ticks) / 10000;            //除10000调整为13位
            return t;
        }
        #endregion
    }
}
